package com.lzx.iteam.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.widget.ImageButton;
import com.lzx.iteam.R;

public class TextImageButton extends ImageButton{

    int textColor;
    String text;
    float textSize;
    int width;
    
    public TextImageButton(Context context, AttributeSet attrs) {
        super(context,attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextImageButton);
        text = ta.getString(R.styleable.TextImageButton_textImageText);
        textColor = ta.getColor(R.styleable.TextImageButton_textImageColor, 0);
        textSize = ta.getDimension(R.styleable.TextImageButton_textImageSize, 15);
        width = ta.getInt(R.styleable.TextImageButton_textImageWidth, 1);
        ta.recycle();
    }

    public TextImageButton(Context context) {
        super(context);
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(text!=null){
            setMaxTextSize();
        }
    }

    public void setMaxTextSize(){
        Paint paint=new Paint();
        paint.setTextSize(textSize);
        FontMetrics fm = new FontMetrics();
        paint.getFontMetrics(fm);
        int height = getHeight();
        int current = (int) (fm.bottom-fm.ascent);
        while(current<height/2){
            textSize++;
            paint.setTextSize(textSize);
            fm = new FontMetrics();
            paint.getFontMetrics(fm);
            current = (int) (fm.bottom-fm.ascent);
        }
        if(text!=null){
            paint.setTextSize(textSize);
            int w = (int) paint.measureText(text);
            int width = getWidth()-getPaddingLeft()-getPaddingRight();
            if(width!=0&&w>width){
                while(w>width){
                    textSize--;
                    paint.setTextSize(textSize);
                    w = (int) paint.measureText(text);
                }
            }
            
        }
        
    }
    
    public void setText(int id){
        String str = getContext().getString(id);
        setText(str);
    }
    
    public void setText(String text){
        if(this.text==null||(this.text!=null&&!this.text.equals(text))){
            this.text = text;
            setMaxTextSize();
            postInvalidate();
        }
       
    }
    /*@Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setMaxTextSize();
    }*/
    
    public void setColor(int color){
        this.textColor = color;
        postInvalidate();
    }
    
    public void setTextSize(int textSize){
        this.textSize = textSize;
        postInvalidate();
    }
    
    public String getText(){
        return text;
    }
   
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(text!=null){
            Paint paint=new Paint();
            paint.setAntiAlias(true);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setColor(textColor);
            paint.setTextSize(textSize);
            FontMetrics fm = paint.getFontMetrics();
            float dy = (fm.descent-fm.ascent)/4;
            canvas.drawText(text, getWidth()/2, getHeight()/2+dy, paint);  //绘制文字
        }
    }
    
}
